﻿using System;
using ApplicationLibrary;
using ApplicationLibrary.SqlConnector;
using Plumber.Accounting.ModuleData;
using Plumber.Sales.ModuleData;
using ApplicationLibrary.CounterManager;
using System.Windows.Forms;

namespace Plumber.Accounting.Forms
{
    public partial class JECreator : ApplicationLibrary.DBManager.BatchForm
    {
        DRJournalEntry drJournalEntry                   = null;
        RRJournalEntryDetail rrJournalEntryDetail       = null;
        RRJournalEntryTaxDetail rrJournalEntryTaxDetail = null;
        CounterManager cm = null;

        public JECreator()
        {
            InitializeComponent();
        }

        protected override void OnAttachData()
        {
            drJournalEntry          = new DRJournalEntry(true);
            rrJournalEntryDetail    = new RRJournalEntryDetail(true);
            rrJournalEntryTaxDetail = new RRJournalEntryTaxDetail(true);

            cntFrom.AttachCounterType(GlobalInfo.GetCounter(Properties.Resources.CN_JournalEntry), GlobalInfo.CurrentDate, null);
            cntTo.AttachCounterType(GlobalInfo.GetCounter(Properties.Resources.CN_JournalEntry), GlobalInfo.CurrentDate, null);
            cntHole.AttachCounterType(GlobalInfo.GetCounter(Properties.Resources.CN_JournalEntry), GlobalInfo.CurrentDate, null);
        }

        protected override void OnBatchExecute()
        {
            if (ckbAnnoPrima.Checked)
                SpostaAnnoPrima();
            if (ckbAnnoDopo.Checked)
                SpostaAnnoDopo();
            if (ckbCompact.Checked)
                CompattaAnno();
            if (ckbMove.Checked)
                MoveJournalNo();
            if (ckbCreateHole.Checked)
                CreateHole();
        }

        private void SpostaAnnoPrima()
        {
            cm = new CounterManager(GlobalInfo.GetCounter(Properties.Resources.CN_JournalEntry), 2012, "aaa");
            QueryBuilder qb = new QueryBuilder();
            using (SqlABCommand scc = new SqlABCommand(GlobalInfo.LoginInfo.ProviderType))
            {
                scc.Connection = GlobalInfo.SqlConnection;

                qb.AddSelectAll(PL_JournalEntry.Name);
                qb.AddFrom(PL_JournalEntry.Name);
                qb.AddCompare(PL_JournalEntry.PostingDate, "'20130101'", "<");
                qb.AddLike(PL_JournalEntry.JournalNo, "13%");
                qb.AddOrder(PL_JournalEntry.JournalNo);

                scc.CommandText = qb.Query;

                SqlABDataReader dr = scc.ExecuteReader();
                try
                {
                    while (dr.Read())
                    {
                        string journalNo = dr.GetValue<string>(PL_JournalEntryDetail.JournalNo);
                        int val = cm.GetNewIntValue();
                        string newjo = cm.CreateCounterFormatted(1);
                        if (drJournalEntry.Find(journalNo))
                        {
                            drJournalEntry.SetValue<string>(PL_JournalEntryDetail.JournalNo, newjo);
                            bool res = drJournalEntry.Update();
                            System.Diagnostics.Debug.Assert(res);
                            if (rrJournalEntryDetail.Find(journalNo))
                            {
                                for (int t = 0; t < rrJournalEntryDetail.Count; t++)
                                    rrJournalEntryDetail.SetValue<string>(PL_JournalEntryDetail.JournalNo, t, newjo);
                                bool result = rrJournalEntryDetail.Update();
                                System.Diagnostics.Debug.Assert(result);
                            }

                            if (rrJournalEntryTaxDetail.Find(journalNo))
                            {
                                for (int t = 0; t < rrJournalEntryTaxDetail.Count; t++)
                                    rrJournalEntryTaxDetail.SetValue<string>(PL_JournalEntryTaxDetail.JournalNo, t, newjo);
                                bool result = rrJournalEntryTaxDetail.Update();
                                System.Diagnostics.Debug.Assert(result);
                            }
                            cm.SetValue(val);
                        }
                    }
                   
                }
                catch (Exception e)
                {
                    if (ApplicationLibrary.Emailer.EmailSender.SendException)
                    {
                        ApplicationLibrary.Emailer.EmailSender.ToAddress = "mauro.rogledi@gmail.com";
                        ApplicationLibrary.Emailer.EmailSender.SendMail(e.Message, e.StackTrace, "");
                    }
                }
                finally
                {
                    dr.Close();
                }
            }
        }
        private void SpostaAnnoDopo()
        {
            CounterFormatter cf = new CounterFormatter();
            cf.AttachCounterType(GlobalInfo.GetCounter(Properties.Resources.CN_JournalEntry), new DateTime(2013, 1, 1));
            cf.PrefixValue = "13";

            QueryBuilder qb = new QueryBuilder();
            using (SqlABCommand scc = new SqlABCommand(GlobalInfo.LoginInfo.ProviderType))
            {
                scc.Connection = GlobalInfo.SqlConnection;

                qb.AddSelectAll(PL_JournalEntry.Name);
                qb.AddFrom(PL_JournalEntry.Name);
                qb.AddCompare(PL_JournalEntry.DocumentDate, "'20130101'", ">=");
                qb.AddLike(PL_JournalEntry.JournalNo, "12%");
                qb.AddOrder(PL_JournalEntry.JournalNo);

                scc.CommandText = qb.Query;

                SqlABDataReader dr = scc.ExecuteReader();
                try
                {
                    while (dr.Read())
                    {
                        string journalNo = dr.GetValue<string>(PL_JournalEntryDetail.JournalNo);
                        //cf.Text = journalNo;
                        cf.CounterNumericValue++;
                        string newjo = cf.ToString();
                        if (drJournalEntry.Find(journalNo))
                        {
                            drJournalEntry.SetValue<string>(PL_JournalEntryDetail.JournalNo, newjo);
                            drJournalEntry.SetValue<DateTime>(PL_JournalEntry.PostingDate, drJournalEntry.GetValue<DateTime>(PL_JournalEntry.DocumentDate));
                            bool res = drJournalEntry.Update();
                            System.Diagnostics.Debug.Assert(res);
                            if (rrJournalEntryDetail.Find(journalNo))
                            {
                                for (int t = 0; t < rrJournalEntryDetail.Count; t++)
                                    rrJournalEntryDetail.SetValue<string>(PL_JournalEntryDetail.JournalNo, t, newjo);
                                bool result = rrJournalEntryDetail.Update();
                                System.Diagnostics.Debug.Assert(result);
                            }

                            if (rrJournalEntryTaxDetail.Find(journalNo))
                            {
                                for (int t = 0; t < rrJournalEntryTaxDetail.Count; t++)
                                    rrJournalEntryTaxDetail.SetValue<string>(PL_JournalEntryTaxDetail.JournalNo, t, newjo);
                                bool result = rrJournalEntryTaxDetail.Update();
                                System.Diagnostics.Debug.Assert(result);
                            }
                        }
                    }

                }
                catch (Exception e)
                {
                    if (ApplicationLibrary.Emailer.EmailSender.SendException)
                    {
                        ApplicationLibrary.Emailer.EmailSender.ToAddress = "mauro.rogledi@gmail.com";
                        ApplicationLibrary.Emailer.EmailSender.SendMail(e.Message, e.StackTrace, "");
                    }
                }
                finally
                {
                    dr.Close();
                }
            }
        }
        private void CompattaAnno()
        {
            cm = new CounterManager(GlobalInfo.GetCounter(Properties.Resources.CN_JournalEntry), ntbAnno.Int, "aaa");
            cm.SetValue(0);
            string like = string.Concat(ntbAnno.Int % 2000, "%");

            QueryBuilder qb = new QueryBuilder();
            using (SqlABCommand scc = new SqlABCommand(GlobalInfo.LoginInfo.ProviderType))
            {
                scc.Connection = GlobalInfo.SqlConnection;

                qb.AddSelectAll(PL_JournalEntry.Name);
                qb.AddFrom(PL_JournalEntry.Name);
                qb.AddLike(PL_JournalEntry.JournalNo, like);
                qb.AddOrder(PL_JournalEntry.JournalNo);

                scc.CommandText = qb.Query;

                SqlABDataReader dr = scc.ExecuteReader();
                try
                {
                    while (dr.Read())
                    {
                        string journalNo = dr.GetValue<string>(PL_JournalEntryDetail.JournalNo);
                        int val = cm.GetNewIntValue();
                        string newjo = cm.CreateCounterFormatted(1);
                        if (drJournalEntry.Find(journalNo))
                        {
                            drJournalEntry.SetValue<string>(PL_JournalEntryDetail.JournalNo, newjo);
                            bool res = drJournalEntry.Update();
                            System.Diagnostics.Debug.Assert(res);
                            if (rrJournalEntryDetail.Find(journalNo))
                            {
                                for (int t = 0; t < rrJournalEntryDetail.Count; t++)
                                    rrJournalEntryDetail.SetValue<string>(PL_JournalEntryDetail.JournalNo, t, newjo);
                                bool result = rrJournalEntryDetail.Update();
                                System.Diagnostics.Debug.Assert(result);
                            }

                            if (rrJournalEntryTaxDetail.Find(journalNo))
                            {
                                for (int t = 0; t < rrJournalEntryTaxDetail.Count; t++)
                                    rrJournalEntryTaxDetail.SetValue<string>(PL_JournalEntryTaxDetail.JournalNo, t, newjo);
                                bool result = rrJournalEntryTaxDetail.Update();
                                System.Diagnostics.Debug.Assert(result);
                            }
                            cm.SetValue(val);
                        }
                    }

                }
                catch (Exception e)
                {
                    if (ApplicationLibrary.Emailer.EmailSender.SendException)
                    {
                        ApplicationLibrary.Emailer.EmailSender.ToAddress = "mauro.rogledi@gmail.com";
                        ApplicationLibrary.Emailer.EmailSender.SendMail(e.Message, e.StackTrace, "");
                    }
                }
                finally
                {
                    dr.Close();
                }
            }
        }
        private void MoveJournalNo()
        {
            if (drJournalEntry.Find(cntTo.Text))
            {
                MessageBox.Show("Destinazione esistente");
                return;
            }

            if (drJournalEntry.Find(cntFrom.Text))
            {
                drJournalEntry.SetValue<string>(PL_JournalEntryDetail.JournalNo, cntTo.Text);
                bool res = drJournalEntry.Update();
                System.Diagnostics.Debug.Assert(res);
                if (rrJournalEntryDetail.Find(cntFrom.Text))
                {
                    for (int t = 0; t < rrJournalEntryDetail.Count; t++)
                        rrJournalEntryDetail.SetValue<string>(PL_JournalEntryDetail.JournalNo, t, cntTo.Text);
                    bool result = rrJournalEntryDetail.Update();
                    System.Diagnostics.Debug.Assert(result);
                }

                if (rrJournalEntryTaxDetail.Find(cntFrom.Text))
                {
                    for (int t = 0; t < rrJournalEntryTaxDetail.Count; t++)
                        rrJournalEntryTaxDetail.SetValue<string>(PL_JournalEntryTaxDetail.JournalNo, t, cntTo.Text);
                    bool result = rrJournalEntryTaxDetail.Update();
                    System.Diagnostics.Debug.Assert(result);
                }
            }
        }
        private void CreateHole()
        {
            int year = 2000+int.Parse(cntHole.PrefixValue);
            cm = new CounterManager(GlobalInfo.GetCounter(Properties.Resources.CN_JournalEntry), year, "aaa");
            CounterFormatter cf = new CounterFormatter();
            cf.AttachCounterType(GlobalInfo.GetCounter(Properties.Resources.CN_JournalEntry), new DateTime(year,1,1));


            QueryBuilder qb = new QueryBuilder();
            using (SqlABCommand scc = new SqlABCommand(GlobalInfo.LoginInfo.ProviderType))
            {
                scc.Connection = GlobalInfo.SqlConnection;

                qb.AddSelectAll(PL_JournalEntry.Name);
                qb.AddFrom(PL_JournalEntry.Name);
                qb.AddCompare<string>(PL_JournalEntry.JournalNo, cntHole.ToString(), ">=");
                qb.AddOrder(PL_JournalEntry.JournalNo, true);

                scc.CommandText = qb.Query;

                SqlABDataReader dr = scc.ExecuteReader();
                bool firstTime = true;
                try
                {
                    while (dr.Read())
                    {
                        string journalNo = dr.GetValue<string>(PL_JournalEntryDetail.JournalNo);
                        cf.Text = journalNo;
                        cf.CounterNumericValue++;
                        string newjo = cf.ToString();
                        if (firstTime)
                        {
                            cm.SetValue(cf.CounterNumericValue);
                            firstTime = false;
                        }

                        if (drJournalEntry.Find(journalNo))
                        {
                            drJournalEntry.SetValue<string>(PL_JournalEntryDetail.JournalNo, newjo);
                            bool res = drJournalEntry.Update();
                            System.Diagnostics.Debug.Assert(res);
                            if (rrJournalEntryDetail.Find(journalNo))
                            {
                                for (int t = 0; t < rrJournalEntryDetail.Count; t++)
                                    rrJournalEntryDetail.SetValue<string>(PL_JournalEntryDetail.JournalNo, t, newjo);
                                bool result = rrJournalEntryDetail.Update();
                                System.Diagnostics.Debug.Assert(result);
                            }

                            if (rrJournalEntryTaxDetail.Find(journalNo))
                            {
                                for (int t = 0; t < rrJournalEntryTaxDetail.Count; t++)
                                    rrJournalEntryTaxDetail.SetValue<string>(PL_JournalEntryTaxDetail.JournalNo, t, newjo);
                                bool result = rrJournalEntryTaxDetail.Update();
                                System.Diagnostics.Debug.Assert(result);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    if (ApplicationLibrary.Emailer.EmailSender.SendException)
                    {
                        ApplicationLibrary.Emailer.EmailSender.ToAddress = "mauro.rogledi@gmail.com";
                        ApplicationLibrary.Emailer.EmailSender.SendMail(e.Message, e.StackTrace, "");
                    }
                }
                finally
                {
                    dr.Close();
                }
            }
        }
    }
}